import { PIC_URL } from './../../providers/Config';

import { Events } from 'ionic-angular';
import { BasePage } from './../../pages/basePage';
import { UserService } from './../../providers/UserService';
import { Component, Injector,Input } from '@angular/core';
import { StorageService } from '../../providers/StorageService';
declare var $: any;

/**
 * Generated class for the ShopBoughtComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'shop-bought',
  templateUrl: 'shop-bought.html'
})
export class ShopBoughtComponent extends BasePage {
  picrooturl ='';  //图片地址
  userinfo={}
  error_msg='暂无已购买美甲信息';
  orderList2=[]   //获取到的待服务订单 2 
  flag:boolean; //显示控制器
  sum:number;   //总单数
  pop=1; // 页码
  bottom1:boolean=false;    //加载全部底部显示器
  //@Input() shopOrderData;  //从shop传入的全部订单
  constructor(injector: Injector, public userService: UserService,public stc:StorageService,public events: Events) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    this.getlist2('');
    events.subscribe('user:created', (hello, time) => {
      if(hello == 0){
        //待服务有数据更新
        console.log("全部有数据更新")
        this.again();
      }
    });
    // this.events.subscribe('shop:created', (status, id) => {
    //   console.log(id);
    //   this.shopList = [];
    //   if (id == 1) {
    //     this.shopList = status;
    //     console.log(this.shopList);
    //     return;
    //   }else{
    //     if(this.shopList.length == 0){
    //       this.noInfo=true;
    //     }else{
    //       this.noInfo=false;
    //     }
    //   }
    // });
  }

  ionViewWillEnter(){
   
  }
  async getlist2(infiniteScroll){
    let param={shop_id:this.userinfo['shop_id'],type_id:2,pop:this.pop}
    let result=await this.userService.shopOrderAPI(param);
    if(result.code == 1){
      if(this.pop==1){
        this.sum=result.count;
      }
      this.pop++;
      this.orderList2=this.orderList2.concat(result.data);
      if(this.orderList2.length ==0){
        this.flag=false;
      }else{
        this.flag=true;
      }
      if(infiniteScroll){
        infiniteScroll.complete(); //请求完成数据更新页面
        if(result.data.length<6){
          $(".box1 ion-infinite-scroll-content").css("display","none");
          this.bottom1=true;
        }
      }
    }else{
      this.flag =false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  }
  //无限滚动
  doInfinite(infiniteScroll) {
    this.bottom1 = false;
    this.getlist2(infiniteScroll);
  }

  //打开拒绝理由页面
  refuse(i){
    this.stc.removeItem("back2");
    let profileModal = this.modalCtrl.create("RefusePage",{item:i,par:1});
    profileModal.onDidDismiss(data => {
      if(data=="ok"){
        this.again();
        let hello1=1;
        this.events.publish("user:created",hello1,Date.now());
      }
    });
    profileModal.present();
  }

  again(){
    this.pop=1;
    this.bottom1 = false;
    this.orderList2=[];
    $(".box1 ion-infinite-scroll-content").css("display","block");
    this.getlist2('');
  }
}
